# -*- coding: UTF-8 -*-

# 声明一个数组
numbers = [5, 7, 1, 3, 0, 2, 6, 9, 4, 8]

# 取得长度
length = len(numbers)

for i in range(1, length):
    tmp = numbers[i]
    leftIndex = 0
    rightIndex = i - 1
    insertIndex = leftIndex
    if numbers[leftIndex] < tmp < numbers[rightIndex]:
        # 数据在范围内才查找
        while leftIndex <= rightIndex:
            mid = int((leftIndex + rightIndex) / 2)
            if tmp < numbers[mid]:
                rightIndex = mid - 1
            else:
                leftIndex = mid + 1

        insertIndex = leftIndex

    elif tmp > numbers[rightIndex]:

        insertIndex = rightIndex + 1

    # 判断是否需要移动位置
    if insertIndex < i:
        for j in range(i - 1, insertIndex - 1, -1):
            numbers[j + 1] = numbers[j]

    numbers[insertIndex] = tmp

print(numbers)
